package com.quanyan.ic.parkingLot.utils;

import com.quanyan.ic.common.utils.DataEncryptionUtils;
import org.springframework.stereotype.Service;

/**
 * 生成海康请求Token
 * Created by zhang on 17/8/22.
 */
@Service
public class HikTokenGenerator {

    public static String generatorGetToken(String address, String path, String param,String hikParkingAppkey,String hikParkingSecret) {

        StringBuilder stringBuilderParam = new StringBuilder();
        stringBuilderParam
                .append(param)
                .append("appkey=")
                .append(hikParkingAppkey)
                .append("&time=")
                .append(System.currentTimeMillis());
        StringBuilder stringBuilderToken = new StringBuilder();
        stringBuilderToken
                .append(path)
                .append(stringBuilderParam)
                .append(hikParkingSecret);
        String token = DataEncryptionUtils.MD5Encode(stringBuilderToken.toString());

        StringBuilder result = new StringBuilder();
        result
                .append(address)
                .append(path)
                .append("?")
                .append(stringBuilderParam)
                .append("?token=")
                .append(token);
        return result.toString();
    }

    public static String generatorPostToken(String address, String path, String param, String hikParkingAppkey, String hikParkingSecret) {
        StringBuilder stringBuilderToken = new StringBuilder();
        stringBuilderToken
                .append(path)
                .append(param)
                .append(hikParkingSecret);
        String token = DataEncryptionUtils.MD5Encode(stringBuilderToken.toString());
        StringBuilder result = new StringBuilder();
        result
                .append(address)
                .append(path)
                .append("&token=")
                .append(token);
        return result.toString();
    }

    public static void main(String[] args) {
        String result = HikTokenGenerator.generatorGetToken("http://119.23.145.198:8082","/webapi/service/eps/getPlatEventTypes","","b425696a","2721bc260a634ddd87613a1e9b535745");
        System.out.println(result);
    }
}
